﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
namespace NCKH.Admin
{
    public partial class themtintuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hientennhomtin();
                hientendanhmuctin();

            }
        }
        protected void hientennhomtin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Manhomtin, Tennhomtin FROM NHOMTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                ddlnhomtin.Items.Add(
                                    new ListItem(rd["Tennhomtin"].ToString(), rd["Manhomtin"].ToString()));
                            }
                        else
                            btnthemtin.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }

        protected void hientendanhmuctin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Madanhmuctin, Tendanhmuctin FROM DANHMUCTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                ddldanhmuctin.Items.Add(
                                    new ListItem(rd["Tendanhmuctin"].ToString(), rd["Madanhmuctin"].ToString()));
                            }
                        else
                            btnthemtin.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected bool kiemtratieude()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string ktten = "SELECT Tieude From NOIDUNGTIN WHERE Tieude=N'" + txttieude.Text + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(ktten, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            lbltb.Text = "Tin này đã tồn tại";
                            // Response.Write("<script>alert('Tên đã bị trùng')</script>");
                            return true;
                        }
                        else
                        {
                            lbltb.Text = "";
                            return false;
                        }

                        rd.Close();
                    }

                }
                cnn.Close();
            }
        }

        protected void btnthemtin_Click(object sender, EventArgs e)
        {
            if (kiemtratieude() == false)
                themnoidungtin();
        }
        protected void themnoidungtin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("themtin", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Madanhmuctin", ddldanhmuctin.SelectedValue);
                    cmd.Parameters.AddWithValue("Tieude", txttieude.Text);
                    cmd.Parameters.AddWithValue("Tomtat", txttomtat.Text);
                    cmd.Parameters.AddWithValue("Noidung", txtnoidung.Text);
                    cmd.Parameters.AddWithValue("Ngaytao", DateTime.Now);
                    cmd.Parameters.AddWithValue("Image", "~/images/" + fileupload1.FileName);
                    fileupload1.SaveAs(Server.MapPath("~/images/" + fileupload1.FileName));
                    cmd.Parameters.AddWithValue("Tag", txtTag.Text);
                    cmd.Parameters.AddWithValue("bhienthi", drhienthi.SelectedValue);
                    cmd.Parameters.AddWithValue("trangthaitin", drdtrangthai.SelectedValue);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                    xoa();

                }
                Response.Redirect("tintuc.aspx");
            }

        }

        ///////////////////////////////////
        public void StorePicture(string filename)
        {
            byte[] imageData = null;
            // Read the file into a byte array
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                imageData = new Byte[fs.Length];
                fs.Read(imageData, 0, (int)fs.Length);
            }
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("themtin", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Madanhmuctin", ddldanhmuctin.SelectedValue);
                    cmd.Parameters.AddWithValue("Tieude", txttieude.Text);
                    cmd.Parameters.AddWithValue("Tomtat", txttomtat.Text);
                    cmd.Parameters.AddWithValue("Noidung", txtnoidung.Text);
                    cmd.Parameters.AddWithValue("Ngaytao", DateTime.Now);
                    cmd.Parameters.Add("Image", SqlDbType.Image);
                    // Store the byte array within the image field
                    cmd.Parameters["@Image"].Value = imageData;
                    cmd.Parameters.AddWithValue("Tag", txtTag.Text);
                    cmd.Parameters.AddWithValue("bhienthi", drhienthi.SelectedValue);
                    cmd.Parameters.AddWithValue("@trangthaitin", drdtrangthai.SelectedValue);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
        }

        public byte[] RetrieveImage()
        {
            byte[] imageData = null;
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                SqlCommand cmd = new SqlCommand("select Image from NOIDUNGTIN", cnn);
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    reader.Read();
                    long bytesize = reader.GetBytes(0, 0, null, 0, 0);
                    // Allocate byte array to hold image data
                    imageData = new byte[bytesize];
                    long bytesread = 0;
                    int curpos = 0;
                    int chunkSize = 1;
                    while (bytesread < bytesize)
                    {
                        // chunkSize is an arbitrary application defined value
                        bytesread += reader.GetBytes(0, curpos, imageData, curpos, chunkSize);
                        curpos += chunkSize;
                    }
                }
                cnn.Close();
            }
            // byte array ‘imageData’ now contains BLOB from database
            return imageData;
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("tintuc.aspx");
        }

        protected void txtnoidung_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txttomtat_TextChanged(object sender, EventArgs e)
        {

        }

        protected void txttieude_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddldanhmuctin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlnhomtin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void txtmatin_TextChanged(object sender, EventArgs e)
        {

        }
        public void xoa()
        {
            txtmatin.Text = "";
            txttieude.Text = "";
            txtTag.Text = "";
            txtnoidung.Text = "";
            txttomtat.Text = "";

        }
    }
}